import {
	regHttpsFront,
	regHttpOrHttpsFront,
	regPassword1
} from "./regulars.js"
import moment from "moment"
moment.locale('zh-cn')

/**
 * 是否为空
 * @param {String | Object | Array} str
 * @returns
 */
export const isEmpty = (str) => {
	return (
		str == null ||
		str == "" ||
		str == undefined ||
		str == "undefined" ||
		str.length == 0 ||
		JSON.stringify(str) == "{}" ||
		/^\s*$/.test(str)
	);
};

// 正则判断字符串全是空格
export const isAllWhitespace = (str) => {
	return /^\s*$/.test(str)
}

/**
 * 手机号码脱敏显示
 */
export const phoneDesen = (phone) => {
	const desReg = /(\d{3})\d*(\d{4})/
	const _str = phone.replace(desReg, '$1****$2');
	return _str
}

/**
 * 昵称脱敏显示
 * @param {Number} maxlength 超过多少长度时处理脱敏
 * @param {Number} subLength 前后截取多少个字符
 */
export const nicknameDesen = (name, maxlength = 6, subLength = 2) => {
	if (name.length < maxlength) {
		return name
	}
	return name.substring(0, subLength) + "**" + name.substring(name.length - subLength, name.length)
}

/**
 * 时间日期格式化
 * @param format
 * @returns {*}
 */
export const dateFormat = (dateObj, format) => {
	let date = {
		'M+': dateObj.getMonth() + 1,
		'd+': dateObj.getDate(),
		'h+': dateObj.getHours(),
		'm+': dateObj.getMinutes(),
		's+': dateObj.getSeconds(),
		'q+': Math.floor((dateObj.getMonth() + 3) / 3),
		'S+': dateObj.getMilliseconds()
	}
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	for (let k in date) {
		if (new RegExp('(' + k + ')').test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length === 1 ?
				date[k] : ('00' + date[k]).substr(('' + date[k]).length))
		}
	}
	return format
}


/**
 * 校验图片地址为https
 * @param {String} url 图片地址
 */
export function checkImageUrl(url) {
	return regHttpsFront.test(url)
}

/**
 * 校验地址为https|http开头
 * @param {String} url 链接
 */
export function checkWebUrl(url) {
	return regHttpOrHttpsFront.test(url)
}

/**
 * 数字按K，w显示
 * @param {Object} url
 */
export function numKFormat(num) {
	if (num >= 1000000) {
		num = numberToFixed((num / 1000) / 10) + 'w'
	}
	// else if (num >= 1000) {
	// 	num = toFixed1((num / 100) / 10) + 'k'
	// }
	return num
}

/**
 * 保留小数位
 */
export function numberToFixed(num, reserve = 1) {
	num = num.toString()
	const index = num.indexOf('.')
	if (index !== -1) {
		num = num.substring(0, index + 2)
		return parseFloat(num).toFixed(reserve)
	} else {
		num = num.substring(0)
		return parseFloat(num)
	}
}

/**
 * 时间戳转换时间
 * @param {Number} tiem 毫秒时间戳
 */
export function timeToFormat(tiem) {
	const today = moment();
	// 今天的凌晨00点
	const todayMidnight = today.startOf('day').valueOf();
	// 昨天的凌晨00点
	const yesterdayMidnight = todayMidnight - (24 * 60 * 60 * 1000);
	// 本周一的凌晨00点
	const mondayMidnight = today.startOf('isoWeek').isoWeekday(1).valueOf();
	// 今年的1月1号凌晨00点
	const yearsMidnight = today.startOf('years').valueOf();
	if (tiem > todayMidnight) {
		return moment(tiem).format('HH:mm')
	} else if (tiem > yesterdayMidnight) {
		return `昨天 ${moment(tiem).format('HH:mm')}`
	} else if (tiem > mondayMidnight) {
		return moment(tiem).format('dddd HH:mm')
	} else if (tiem > yearsMidnight) {
		return moment(tiem).format('M月D日 HH:mm')
	}
	return moment(tiem).format('YYYY年M月D日 HH:mm')
}

/**
 * 验证邮箱格式
 */
export function validateEmail(email) {
	const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
	return emailRegex.test(email);
}

/**
 * 验证密码格式
 */
export function validatePassword(password) {
	return regPassword1.test(password);
}